package hk.linkcredit.awardservice.award.controller;

import hk.linkcredit.awardservice.award.domain.vo.ActivityAggregateVo;
import hk.linkcredit.awardservice.award.domain.entity.AwardRecord;
import hk.linkcredit.awardservice.award.service.IAwardActivityService;
import hk.linkcredit.awardservice.award.service.IAwardRecordService;
import hk.linkcredit.awardservice.rocketmq.AwardProducer;
import hk.linkcredit.awardservice.utils.model.Result;
import lombok.AllArgsConstructor;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 奖励活动Controller
 *
 * @author african-loan
 * @date 2022-06-22
 */
@RestController
@AllArgsConstructor
public class AwardActivityController {

    private IAwardActivityService awardActivityService;

    private IAwardRecordService awardRecordService;

    private AwardProducer awardProducer;

    /**
     * 获取分销活动
     *
     * @return
     */
    @GetMapping("/activity/detail")
    public Result getDistributionActivity() {
        ActivityAggregateVo distributionActivity = awardActivityService.getDistributionActivity();
        return Result.success(distributionActivity);
    }

    /**
     * 获取客户总奖励金
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userId}/amountSum")
    public Result getAmountSum(@PathVariable(name = "userId") String userId) {
        return Result.success(awardRecordService.getAmountSum(userId));
    }

    /**
     * 获取客户奖励金流水
     *
     * @param userId
     * @return amountSum:总流水  awardRecords
     */
    @GetMapping("/record/{userId}")
    public Result getUserRecord(@PathVariable(name = "userId") String userId) {
        return Result.success(awardRecordService.getUserRecordInfo(userId));
    }


    /**
     * 获取客户邀请奖励金流水（受邀号码进行分组）
     *
     * @param userId
     * @return awardAmount、invitedPhone、invitedUserId
     */
    @GetMapping("/record/invite/{userId}")
    public Result getUserInviteRecord(@PathVariable(name = "userId") String userId) {
        return Result.success(awardRecordService.getUserInviteRecord(userId));
    }


    /**
     * 奖励的次数
     *
     * @param bvnAccount
     * @param actId
     * @param awardConfigId
     * @return
     */
    @GetMapping("/record/rewardedTotal")
    public Result rewardedTotal(@RequestParam String bvnAccount, @RequestParam Long actId, @RequestParam Long awardConfigId) {
        return Result.success(awardRecordService.rewardedTotal(bvnAccount, actId, awardConfigId));
    }

    /**
     * 触发奖励
     *
     * @param recordList
     * @return
     */
    @PostMapping("/distribution")
    public Result awardDistribution(@RequestBody List<AwardRecord> recordList) {
        TransactionSendResult result = awardProducer.sendAwardMessageInTransaction(recordList);
        return Result.success(result.getLocalTransactionState());
    }


//    @GetMapping("/doTest")
//    public void doTest22() {
//        doTest();
//    }
//
//    public void doTest(){
//        List<AwardRecord> objects = new ArrayList<>();
//
//        AwardRecord record = new AwardRecord();
//        record.setSerialNo("sjsjsjjjjjjjjjj111");
//        record.setUserId("295c6e93f71846c3ad71ccb2a7825944");
//        record.setCustNo("2022022106181457920");
//        record.setRealBvn("22544975038");
//        record.setRegisterPhone("08163647038");
//        record.setAwardAmount("100000");
//
//        record.setInvitedPhone("ie02099");
//        record.setInvitedUserId("50dcbd46015d43979c90baf31b3f16f7");
//        record.setInvitedBvn("22458372222");
//
//        record.setInvitedBvn("22200000000");
//        record.setActId(1L);
//        record.setAwardConfigId(1L);
//        record.setInvitedEventType("0");
//        record.setInvitedEventAwardNum(0);
//        record.setInvitedEventAwardMethod("0");
//        record.setInvitedEventAwardValue(100);
//        objects.add(record);
//        TransactionSendResult result = awardProducer.sendAwardMessageInTransaction(objects);
//        System.out.println("出参结果："+result.toString());
//    }


}
